package com.education.welco.service.impl;

import com.education.common.utils.Convert;
import com.education.common.utils.StringUtils;
import com.education.welco.mapper.VirtualRealityMapper;
import com.education.welco.pojo.VirtualReality;
import com.education.welco.pojo.vo.VirtualRealityVO;
import com.education.welco.service.IVirtualRealityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 虚拟仿真 服务层实现
 *
 * @Author: liftsail
 * @Date: 2023-04-13
 * @Description: 不积跬步无以至千里
 */
@Service
public class VirtualRealityServiceImpl implements IVirtualRealityService {

    @Autowired
    private VirtualRealityMapper virtualRealityMapper;

    /**
     * 查询虚拟仿真信息
     *
     * @param id 虚拟仿真ID
     * @return 虚拟仿真信息
     */
    @Override
    public VirtualReality selectVirtualRealityById(Integer id) {
        return virtualRealityMapper.selectVirtualRealityById(id);
    }

    /**
     * 查询虚拟仿真列表
     *
     * @param virtualReality 虚拟仿真信息
     * @return 虚拟仿真集合
     */
    @Override
    public List<VirtualReality> selectVirtualRealityList(VirtualReality virtualReality) {
        return virtualRealityMapper.selectVirtualRealityList(virtualReality);
    }

    /**
     * 新增虚拟仿真
     *
     * @param virtualReality 虚拟仿真信息
     * @return 结果
     */
    @Override
    public int insertVirtualReality(VirtualReality virtualReality) {
        virtualReality.setCreateTime(new Date());
        return virtualRealityMapper.insertVirtualReality(virtualReality);
    }

    /**
     * 修改虚拟仿真
     *
     * @param virtualReality 虚拟仿真信息
     * @return 结果
     */
    @Override
    public int updateVirtualReality(VirtualReality virtualReality) {
        return virtualRealityMapper.updateVirtualReality(virtualReality);
    }

    /**
     * 删除虚拟仿真对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteVirtualRealityByIds(String ids) {
        return virtualRealityMapper.deleteVirtualRealityByIds(Convert.toStrArray(ids));
    }

    @Override
    public Integer selectVirtualRealitySchool(Integer id) {
        return virtualRealityMapper.selectVirtualRealitySchool(id);
    }

    @Override
    public List<VirtualReality> schoolVirtualRealityList(Integer schoolId) {
        return virtualRealityMapper.schoolVirtualRealityList(schoolId);
    }

    @Override
    public Integer updateSchoolShareVR(String vrIds, Integer schoolId) {
        Integer i = virtualRealityMapper.deleteVirtualRealitySchoolBySchoolId(schoolId);
        if (StringUtils.isBlank(vrIds)) {
            return 0;
        }
        List<String> vrIdListStr = Arrays.asList(Convert.toStrArray(vrIds));
        List<Integer> vrIdList = vrIdListStr.stream().map(id -> Integer.valueOf(id)).collect(Collectors.toList());
        return virtualRealityMapper.insertSchoolShareVR(vrIdList, schoolId);
    }

    @Override
    public List<VirtualReality> selectSchoolShareVRList(Integer schoolId) {
        return virtualRealityMapper.selectSchoolShareVRList(schoolId);
    }

    @Override
    public List<VirtualRealityVO> selectCatalogShareVRList(Integer schoolId, Integer catalogId) {
        return virtualRealityMapper.selectCatalogShareVRList(schoolId, catalogId);
    }

    @Override
    public Integer updateCatalogShareVR(String vrIds, Integer teaUserId, Integer catalogId) {
        Integer i = virtualRealityMapper.deleteVirtualRealityCatalogByCatalogId(catalogId);
        if (StringUtils.isBlank(vrIds)) {
            return 0;
        }
        List<String> vrIdListStr = Arrays.asList(Convert.toStrArray(vrIds));
        List<Integer> vrIdList = vrIdListStr.stream().map(id -> Integer.valueOf(id)).collect(Collectors.toList());
        return virtualRealityMapper.insertCatalogShareVR(vrIdList, teaUserId, catalogId);
    }

    @Override
    public Integer selectSchoolIdBycatalogId(Integer lessonId) {
        return virtualRealityMapper.selectSchoolIdBycatalogId(lessonId);
    }
}